package com.yayan.beautyclub.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mysql.jdbc.Statement;
import com.yayan.beautyclub.bean.User;

public class UserDao extends ConnectionBase implements IUserDao {

	public UserDao(){}
	
	@Override
	public User findUserById(long uId) throws Exception {
		// TODO Auto-generated method stub
		QueryRunner qRunner = new QueryRunner();
		String sqlStr = "select uName,uPhone,nickName,avatarUrl,gender,province,city,country from users where uId=?";
		User user = (User) qRunner.query(getConn(), sqlStr, new BeanHandler(User.class), uId);
		return user;
	}

	@Override
	public User findUserByopenId(String openId) throws Exception {
		// TODO Auto-generated method stub
		QueryRunner qRunner = new QueryRunner();
		String sqlStr = "select * from users where openId=?";
		User user = (User) qRunner.query(getConn(), sqlStr, new BeanHandler(User.class), openId);
		return user;
	}

	@Override
	public long save(String openId) throws Exception {
		// TODO Auto-generated method stub
		String sqlStr = "insert into users(openId) values(?)";
		Connection conn = getConn();
		conn.setAutoCommit(false);       //不自动提交  
		PreparedStatement ps= conn.prepareStatement(sqlStr,Statement.RETURN_GENERATED_KEYS);
		ps.setString(1, openId);
		int re = ps.executeUpdate();
		ResultSet rsKey = ps.getGeneratedKeys();
		long r=0;
		while(re!=0&&rsKey.next())
		{
			r = rsKey.getLong(1);
		}
		conn.commit();
		conn.setAutoCommit(true);     //设回自动提交  
		close(conn,ps,rsKey);
		return r;
	}

	@Override
	public boolean update(long uId, User user) throws Exception {
		// TODO Auto-generated method stub
		String sqlStr = "update users set nickName=?,avatarUrl=?,gender=?,province=?,city=?,country=? where uId=?";
		Connection conn = getConn();		
		PreparedStatement ps = conn.prepareStatement(sqlStr);
		ps.setString(1, user.getNickName());
		ps.setString(2, user.getAvatarUrl());
		ps.setInt(3, user.getGender());
		ps.setString(4, user.getProvince());
		ps.setString(5, user.getCity());
		ps.setString(6, user.getCountry());
		ps.setLong(7, uId);
		boolean r = ps.executeUpdate() > 0 ? true:false;
		close(conn,ps,null);
		return r;
	}

	@Override
	public List<User> findUserByCondition(int page, int size) throws Exception {
		// TODO Auto-generated method stub
		String sqlStr = "select * from users limit " + page*size + "," + size;
		QueryRunner qRunner = new QueryRunner();
		List<User> list = (List<User>) qRunner.query(getConn(), sqlStr, new BeanListHandler(User.class));
		return list;
	}

	@Override
	public boolean setPhoneAndName(long uId, User user) throws Exception {
		// TODO Auto-generated method stub
		String sqlStr = "update users set uPhone=?,uName=? where uId=?";
		Connection conn = getConn();		
		PreparedStatement ps = conn.prepareStatement(sqlStr);
		ps.setString(1, user.getuPhone());
		ps.setString(2, user.getuName());		
		ps.setLong(3, uId);
		boolean r = ps.executeUpdate() > 0 ? true:false;
		close(conn,ps,null);
		return r;
	}

}
